package com.ccys.cms.controller.productAndService;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.PackageDetailService;
import com.ccys.cms.service.productAndService.PackageService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.productAndService.BatchPackageDetailDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.PackageDto;
import com.ccys.db.entity.TblPackage;
import com.ccys.db.entity.TblPackageDetail;
import com.ccys.db.mapper.TblPackageDetailMapper;
import com.ccys.db.mapper.TblPackageMapper;
import com.ccys.db.vo.web.productAndService.IdNameVo;
import com.ccys.db.vo.web.productAndService.MedicalServiceCategoryVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "打包管理")
public class PackageController {


    @Resource
    private PackageService service;


    @Resource
    private PackageDetailService packageDetailService;

    @Resource
    private TblPackageDetailMapper detailMapper;

    @Resource
    private TblPackageMapper mapper;

    @PostMapping("package")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(@RequestBody BatchPackageDetailDto dto) {

        return service.change(dto);
    }


    @PutMapping("package")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(@RequestBody BatchPackageDetailDto dto) {

        return service.change(dto);
    }


    @PutMapping("package/status")
    @ApiOperation("修改状态")
    @Transactional
    public ResponseResult update(String id,Integer status) {
        TblPackage model = new TblPackage();
        model.setId(id);
        model.setStatus(status);
        int i = service.insertOrUpdate(model);
        return ResponseResult.update(i);
    }


    @GetMapping("package/detail")
    @ApiOperation("根据id查询")
    public ResponseResult<BatchPackageDetailDto, Object> getById(String id) {


        TblPackage entityById = service.findEntityById(id);
        //根据packageId查询packageDetail
        List<TblPackageDetail> detailList = detailMapper.getPackageDetailList(id);
        BatchPackageDetailDto dto = new BatchPackageDetailDto();
        BeanUtils.copyProperties(entityById, dto);
        dto.setDetailList(detailList);
        return ResponseResult.success(dto);
    }


    @DeleteMapping("package")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblPackage model = new TblPackage();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //删除对应的packagedetial
        packageDetailService.deletePackageDetails(id);
        return ResponseResult.update(i);
    }


    @GetMapping("package")
    @ApiOperation(value = "查询分页", notes = "排序字段 t1.name / t1.items / t1.price /  t1.status /t2. user_fullname / t1.created_on ")
    public ResponseResult<PageInfo<MedicalServiceCategoryVo>, Object> getPage(PackageDto dto) {

        return service.getPage(dto);
    }




    @GetMapping("package/list")
    @ApiOperation(value = "根据类型查询package集合",notes = "type字段传值：service | medication")
    public ResponseResult<List<IdNameVo>,Object> getList(String type){
        List<IdNameVo> list = mapper.getPackageList(type);
        return ResponseResult.success(list);
    }

}
